﻿
using System;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
namespace Time.OPCServerV
{
    public class MyFlie
    {
        public static void DeleteLog()
        {
            try
            {
                string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
                string path = directoryName.Substring(6, directoryName.Length - 6) + @"\log\";
                string[] files = Directory.GetFiles(path);
                string[] directories = Directory.GetDirectories(path);
                string s = DateTime.Now.ToString("yyyy-MM-dd");
                string str5 = "";
                int logDays = GetLogDays();
                if ((logDays != -1) && (files.Length > logDays))
                {
                    for (int i = 0; i < files.Length; i++)
                    {
                        str5 = files[i].Substring(path.Length, files[i].Length - path.Length);
                        string str4 = str5.Substring(3, 10);
                        if (((((str5.Substring(0, 3) == "log") && (str5.Substring(7, 1) == "-")) && ((str5.Substring(10, 1) == "-") && (str5.Substring(13, 4) == ".txt"))) && (IsNumber(str4.Substring(0, 4)) && IsNumber(str4.Substring(5, 2)))) && IsNumber(str4.Substring(8, 2)))
                        {
                            TimeSpan span = (TimeSpan)(DateTime.Parse(s).Date - DateTime.Parse(str4).Date);
                            if (span.Days < 0)
                            {
                                File.Delete(files[i]);
                                WriteLog("MCT日志文件中含有非法文件：“" + files[i] + "”，已被删除！");
                            }
                            else
                            {
                                span = (TimeSpan)(DateTime.Parse(s).Date - DateTime.Parse(str4).Date);
                                if (span.Days >= logDays)
                                {
                                    File.Delete(files[i]);
                                }
                            }
                        }
                        else
                        {
                            File.Delete(files[i]);
                            WriteLog("MCT日志文件中含有非法文件：“" + files[i] + "”，已被删除！");
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                WriteLog(exception.Message);
            }
        }

        private static int GetLogDays()
        {
            return 7;
        }

        public static bool IsNumber(string str_number)
        {
            return Regex.IsMatch(str_number, "^[0-9]*$");
        }

        public static void WriteLog(string strLog)
        {
            StreamWriter writer = null;
            try
            {
                lock (typeof(MyFlie))
                {
                    string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
                    string path = directoryName.Substring(6, directoryName.Length - 6) + @"\log\";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    DateTime time = new DateTime();
                    string str2 = "log" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    string str3 = path + str2;
                    if (!File.Exists(str3))
                    {
                        writer = File.CreateText(str3);
                    }
                    else
                    {
                        writer = File.AppendText(str3);
                    }
                    string str5 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                    if (strLog != "")
                    {
                        writer.WriteLine(str5 + " " + strLog);
                    }
                    else
                    {
                        writer.WriteLine("");
                    }
                    writer.Flush();
                    writer.Close();
                    writer.Dispose();
                }
            }
            catch
            {
                writer.Flush();
                writer.Close();
                writer.Dispose();
                WriteLogError(strLog);
            }
        }

        public static void WriteLogError(string strLog)
        {
            StreamWriter writer = null;
            try
            {
                lock (typeof(MyFlie))
                {
                    string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
                    string path = directoryName.Substring(6, directoryName.Length - 6) + @"\log\";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    DateTime time = new DateTime();
                    string str2 = "log" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    string str3 = path + str2;
                    if (!File.Exists(str3))
                    {
                        writer = File.CreateText(str3);
                    }
                    else
                    {
                        writer = File.AppendText(str3);
                    }
                    string str5 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                    if (strLog != "")
                    {
                        writer.WriteLine(str5 + " " + strLog);
                    }
                    else
                    {
                        writer.WriteLine("");
                    }
                    writer.Flush();
                    writer.Close();
                    writer.Dispose();
                }
            }
            catch
            {
                writer.Flush();
                writer.Close();
                writer.Dispose();
            }
        }
    }
}

